package com.controller;

import com.entity.Room;
import com.service.RoomService;
import com.vo.RoomVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Controller
public class RoomController {

    @Autowired
    private RoomService roomService;


    @RequestMapping("/room")
    public String room(){
        return "room";
    }

    @RequestMapping("/roomAll")
    @ResponseBody
    public Map<String, Object> roomAll(@RequestParam int page,@RequestParam int limit) {
//        System.out.println(page+","+limit);
        List<RoomVo> rooms = roomService.roomAll(page*limit-limit, limit);

        Map<String, Object> map = new LinkedHashMap<>();
        map.put("code", 0);
        map.put("msg", "数据错误");
        map.put("count", roomService.count());
        map.put("data", rooms);

        return map;
    }

    @GetMapping("/show/{roomId}")
    @ResponseBody
    public List<String> show(@PathVariable("roomId") String roomId) {
        List<String> list = new ArrayList<>();
        list.add(roomService.situation(roomId));
        return list;
    }

    @PostMapping("/roomInsert")
    public String roomInsert(String id, String name, int row, int column, Model model){
        int i = roomService.insertRoom(id,name,row,column);
        if (i == 1 ){
            return "room";
        }
        model.addAttribute("ret","添加失败");
        return "error";
    }
}
